package dp;

public class P413 {
    public int numberOfArithmeticSlices(int[] nums) {
        if (nums.length <= 2) {
            return 0;
        }

        int total = 0;
        int left = 0, right = 1;

        int diff = nums[right] - nums[left];
        while (right + 1 < nums.length) {
            if (nums[right + 1] - nums[right] == diff) {//相等右移
                right++;
            } else {//不等
                int len = right - left + 1;
                if(len>=3){
                    for (int i = 3; i <= len; i++) {
                        total += len - i + 1;
                    }
                }
                //重新定界
                left = right;
                diff = nums[right + 1] - nums[right];
                right++;
            }
        }
        if (left != right) {
            int len = right - left + 1;
            if(len>=3){
                for (int i = 3; i <= len; i++) {
                    total += len - i + 1;
                }
            }
        }
        return total;
    }

    public static void main(String[] args) {
        int total = new P413().numberOfArithmeticSlices(new int[]{
                1, 2, 3, 4
        });
        System.out.println(total);
    }
}
